package XComponent.show
{
	import fl.core.UIComponent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class XEmphasizedBorder		extends EventDispatcher
	{	
		static public const  NORMAL : int = 0; 
		static public const  STATE_DRAG : int = 1;
		static public const  STOPDRAGRECTDOT : int = 3;
		
		static public const	DRAGSTATE_LEFT : int = 5;
		static public const 	DRAGSTATE_RIGHT : int = 6;
		static public const	DRAGSTATE_UP : int = 7;
		static public const 	DRAGSTATE_DOWN : int = 8;
		
		static public var  STATE_CHANGDED:String = "STATE_CHANGDED";
		
		public var dots : Array;
		
		private  var m_pan : *;
		
		public var curState : int;
		
		private var m_dragXState : int ;
		private var m_dragYState : int ;
		
		public function XEmphasizedBorder()
		{
		}
		
		public function setEmphasizedBorder(entity:*,type:int=0):void
		{
			dots = [];
			
			var pan : * = entity ;
			
			if(pan)
			{	
				m_pan = pan;
			
				drawBorderLine();
				
				addRectDot(pan,0,0);
				addRectDot(pan,pan.width/2,0);
				addRectDot(pan,pan.width,0);
				addRectDot(pan,pan.width,pan.height/2);
				addRectDot(pan,pan.width,pan.height);
				addRectDot(pan,pan.width/2,pan.height);
				addRectDot(pan,0,pan.height);
				addRectDot(pan,0,pan.height/2);
				
				addRectDot(pan,pan.width/2,pan.height/2,0x0000FF);
			}
			
			switch(type)
			{
				case NORMAL:
					break;
			}
		}
		
		private function drawBorderLine() : void
		{
			if(m_pan is Bitmap)
			{
				
			}
			else
			{
				m_pan.graphics.clear();
				
				m_pan.graphics.lineStyle(1,0xFF0000);
				
				m_pan.graphics.moveTo(0,0);
				m_pan.graphics.lineTo(m_pan.width,0);
				m_pan.graphics.lineTo(m_pan.width,m_pan.height);
				m_pan.graphics.lineTo(0,m_pan.height);
				m_pan.graphics.lineTo(0,0);
			}
		}
		
		private function addRectDot(pan:*,dotX:Number,dotY:Number,color:int= -1) : void 
		{
			if(pan)
			{
				var dot : XRectDot = new XRectDot;
				dot.drawDot(color);
				
				setRectDotPosition(dot,dotX,dotY);
				
				dot.buttonMode = true;
				
				pan.addChild(dot);
				dots.push(dot);
			}
		}
		
		public function setRectDotPosition(rectDot : XRectDot,orgX:Number,orgY:Number) : void
		{
			rectDot.x = orgX - rectDot.width/2;
			rectDot.y = orgY - rectDot.height/2;
		}
		
		public function autoSize() : void
		{
			drawBorderLine();
			
			setRectDotPosition(dots[1],m_pan.width/2,0);
			setRectDotPosition(dots[2],m_pan.width,0);
			setRectDotPosition(dots[3],m_pan.width,m_pan.height/2);
			setRectDotPosition(dots[4],m_pan.width,m_pan.height);
			setRectDotPosition(dots[5],m_pan.width/2,m_pan.height);
			setRectDotPosition(dots[6],0,m_pan.height);
			setRectDotPosition(dots[7],0,m_pan.height/2);
			
			setRectDotPosition(dots[8],m_pan.width/2,m_pan.height/2);
		}
		
		public function checkRectDotType(dot:XRectDot=null) : int
		{	
			if(!dots) return -1;
			
			m_dragXState = m_dragYState = curState = 0;
			
			var rec : Rectangle = new Rectangle(m_pan.width/2 - 3,m_pan.height/2-3,10,10);
			
			if(rec.contains(m_pan.mouseX,m_pan.mouseY))
			{
				curState = STATE_DRAG;
						
				return curState;
			}
					
			if(m_pan.mouseX < 10 && m_pan.mouseX > -5)
			{
				m_dragXState = DRAGSTATE_LEFT;
			}
			else if(m_pan.mouseX > m_pan.width-10 && m_pan.mouseX < m_pan.width+5)
			{
				m_dragXState = DRAGSTATE_RIGHT;
			}
	
					
			if(m_pan.mouseY < 10 && m_pan.mouseY > -5)
			{
				m_dragYState = DRAGSTATE_UP;
			}
			else if(m_pan.mouseY > m_pan.height-10 && m_pan.mouseY < m_pan.height+5)
			{
				m_dragYState = DRAGSTATE_DOWN;
			}
			
			return -1;
		}
		
		public function destory() : void
		{
			for ( var i:int = 0;i<dots.length;i++)
			{
				dots[i].destory();
				m_pan.removeChild(dots[i]);
			}
			
			if(m_pan)
			{
				m_pan.graphics.clear();
			}
			
			dots = 			null;
			m_pan = 		null;
		}
		
		public function OnMDRectDot(evt : MouseEvent) : void
		{
			evt.stopImmediatePropagation();
			
			checkRectDotType(evt.currentTarget as XRectDot);
			
			dispatchEvent(new Event(STATE_CHANGDED));
		}
		
		public function OnMURectDot(evt : MouseEvent) : void
		{
			curState = STOPDRAGRECTDOT;
			
			m_dragXState = 0;
			m_dragYState = 0;
			
			dispatchEvent(new Event(STATE_CHANGDED));
		}
		
		public function get DragXState() : int
		{
			return m_dragXState;
		}
		
		public function get DragYState() : int
		{
			return m_dragYState;
		}
	}
}